package mahjong.auto.com.automahjong;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import mahjong.auto.com.os.Mahjong;

public class AppService extends Service {
    public static final int STATUS_START = 8;
    public static final int STATUS_STOP = 9;
    private App app;
    public EnumPublic mStauts;

    public AppService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = (App) getApplication();
        app.setHandler(handler);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (EnumPublic.Start == mStauts) {
                Mahjong.discern(app.getCapture().getBitmap());
                handler.postDelayed(runnable, 1000);
            }
        }
    };

    private Handler handler = new Handler() {

        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);

            if (EnumPublic.Start.ordinal() == msg.what) {
                app.getCapture().startVirtual();
                mStauts = EnumPublic.Start;
                handler.post(runnable);

            } else if (EnumPublic.Stop.ordinal() == msg.what) {
                app.getCapture().stopVirtual();
                mStauts = EnumPublic.Stop;
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
